﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MLOCore
{
    public class MergedLibrary
    {

        private OutputFileCollection union;
        public OutputFileCollection Union
        {
            get
            {
                union = union ?? new OutputFileCollection();
                return union;
            }
        }

        private OutputFileCollection complement;
        public OutputFileCollection Complement
        {
            get
            {
                complement = complement ?? new OutputFileCollection();
                return complement;
            }
        }

        public void PerformMerge()
        {
            // TODO:Implement merge
        }

        public static MergedLibrary Merge(Library library1, Library library2, Single matchThreshold)
        {
            Guard.IsNotNull(library1, "library1");
            Guard.IsNotNull(library2, "library2");

            MergedLibrary result = new MergedLibrary();

            IEnumerable<SongFile> sfUnion = library1.Files.Union<SongFile>(library2.Files, new SongFileComparer(matchThreshold));

            foreach (SongFile sf in sfUnion)
            {
                result.Union.Add(new OutputSongfile(sf));
            }

            DetermineComplement(result, library1, sfUnion);

            DetermineComplement(result, library2, sfUnion);

            return result;
        }

        private static void DetermineComplement(MergedLibrary result, Library library1, IEnumerable<SongFile> sfUnion)
        {
            IEnumerable<SongFile> missing = library1.Files.SkipWhile<SongFile>(s => sfUnion.Contains(s));

            foreach (SongFile sf in missing)
            {
                result.Complement.Add(new OutputSongfile(sf));
            }
            
        }

    }

}
